package com.chinacoal.hr.levelimpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.client.AttendanceAppealClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.AttendanceAppeal;
import com.chinacoal.hr.levelapi.entity.AttendanceAppealDetail;
import com.chinacoal.hr.levelimpl.service.AttendanceAppealDetailService;
import com.chinacoal.hr.levelimpl.service.AttendanceAppealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-14 11:37
 * @description: 考勤申诉信息controller
 **/
@RestController
public class AttendanceAppealController implements AttendanceAppealClient{
    @Autowired
    private AttendanceAppealService attendanceAppealService;

    @Autowired
    private AttendanceAppealDetailService attendanceAppealDetailService;

    /**
     * 查询考勤申诉信息
     * @param page  :当前页数
     * @param limit :每页条数
     * @param orgCode :组织编码
     * @param yrmonth :月份
     * @param empCode :员工姓名或编号
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/14 2019/11/14
     */
    public Result list(Long page,
                       Long limit,
                       String orgCode,
                       String yrmonth,
                       String empCode,
                       String explainCode) {
        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("orgCode", orgCode);
        condition.put("yrmonth", yrmonth);
        condition.put("empCode", empCode);
        condition.put("explainCode", explainCode);

        IPage<AttendanceAppeal> recordList = attendanceAppealService.getAttendanceAppealList(condition);
        return ResultGenerator.genOkResult(recordList);
    }


    /**
     * 查询考勤申诉下的考勤申诉明细数据
     *
     * @param explainId :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/28 2019/11/28
     */
    public Result list(String explainId) {
        List<AttendanceAppealDetail> attendanceAppealDetailList = null;
        try {
            attendanceAppealDetailList = this.attendanceAppealDetailService.getAttendanceAppealDetailListByAppealId(explainId);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult(attendanceAppealDetailList);
    }

    /**
     * 新增考勤申诉单
     *
     * @param record :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/21 2019/11/21
     */
    public Result add(@RequestBody AttendanceAppeal record) {
        Map<String, String> result = null;

        try {
            result = attendanceAppealService.addAttendanceAppeal(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResultGenerator.genFailedResult(throwable.getMessage());
        }

        return ResultGenerator.genOkResult();
    }

    /**
     * 编辑考勤申诉单
     *
     * @param explainId        :
     * @param attendanceAppeal :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */

    public Result update( String explainId, AttendanceAppeal attendanceAppeal) {
        Map<String, String> result = null;
        try {
            result = this.attendanceAppealService.updateAttendanceAppeal(attendanceAppeal);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult();
    }

    /**
     * 删除考勤申诉
     *
     * @param explainId :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/26 2019/11/26
     */
    public Result delete(String explainId) {
        try {
            Map<String, String> result = attendanceAppealService.deleteAttendanceAppeal(explainId);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 提交考勤申诉
     *
     * @param explainId :
     * @param record    :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/26 2019/11/26
     */

    public Result submit( String explainId,
                         AttendanceAppeal record) {
        try {
            Map<String, String> result = attendanceAppealService.submitAttendanceAppeal(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 撤回考勤申诉
     * @author : YunTao.Li
     * @date : 2019/12/12 2019/12/12
     * @param explainId :
     * @param record :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     */

    public Result drawback( String explainId, AttendanceAppeal record) {
        try {
            Map<String, String> result = attendanceAppealService.drawbackAttendanceAppeal(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }
}
